//
//  ViewController.m
//  UI_homework_05_封装小相册
//
//  Created by li cheng on 16/7/20.
//  Copyright © 2016年 li cheng. All rights reserved.
//

#import "ViewController.h"
#define HEIGHT self.view.frame.size.height
#define WIDTH self.view.frame.size.width

@interface ViewController ()<UIScrollViewDelegate>

@property (nonatomic, retain) UIScrollView *scrollViewOfAlbum;
@property (nonatomic, retain) UIScrollView *scrollView;
@property (nonatomic, retain) NSArray *arrayOfImage;
@property (nonatomic, retain) UIPageControl *page;

@end


@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    // 创建一个最大的ScrollView
    self.scrollViewOfAlbum = [[UIScrollView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.scrollViewOfAlbum.delegate = self;
    [self.view addSubview:self.scrollViewOfAlbum];
    // 创建装照片的数组的名字
    self.arrayOfImage = [NSArray arrayWithObjects:@"4", @"1", @"2", @"3", @"4", @"1", nil];
    for (int i = 0; i < self.arrayOfImage.count; i++) {
        // 创建小的ScrollView用来放照片
        self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(i * WIDTH, 0, WIDTH, HEIGHT)];
        // 要签代理才能放大缩小
        self.scrollView.delegate = self;
        self.scrollView.minimumZoomScale = 0.5;
        self.scrollView.maximumZoomScale = 2.0;
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
        imageView.image = [UIImage imageNamed:self.arrayOfImage[i]];
        [self.scrollView addSubview:imageView];
        [self.scrollViewOfAlbum addSubview:self.scrollView];
        
    }
    
    self.scrollViewOfAlbum.contentSize = CGSizeMake(WIDTH * self.arrayOfImage.count, 0);
    self.scrollViewOfAlbum.contentOffset = CGPointMake(WIDTH, 0);
    self.scrollViewOfAlbum.pagingEnabled = YES;
    
    
    // 创建分页控制器
    self.page = [[UIPageControl alloc] initWithFrame:CGRectMake(207 - 150, 600, 300, 60)];
    self.page.numberOfPages = _arrayOfImage.count - 2;
    [self.page addTarget:self action:@selector(pageAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.page];
}

- (void)pageAction:(UIPageControl *)page{
    // 点击事件
    [self.scrollViewOfAlbum setContentOffset:CGPointMake(WIDTH * (self.page.currentPage + 1), 0) animated:YES];
}

// 想要滑动必写的代理方法
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
    return [scrollView.subviews firstObject];
}

// 当滑动结束的时候跳转
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    if (self.scrollViewOfAlbum.contentOffset.x >= (self.arrayOfImage.count - 1) * WIDTH) {
        self.scrollViewOfAlbum.contentOffset = CGPointMake(WIDTH, 0);
        self.page.currentPage = 2;
    } else if (self.scrollViewOfAlbum.contentOffset.x <= 0) {
        self.scrollViewOfAlbum.contentOffset = CGPointMake( (self.arrayOfImage.count - 2) * WIDTH, 0);
        self.page.currentPage = 4;
    }
    // 当滑动scrollView的时候,当前的page跟着改变
    self.page.currentPage = (self.scrollViewOfAlbum.contentOffset.x - WIDTH) / WIDTH;
    
    for (UIScrollView *smallScrollView in scrollView.subviews) {
        if ([smallScrollView isKindOfClass:[UIScrollView class]]) {
            
            smallScrollView.zoomScale = 1;
        }
    }
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
